package com.smz.controller;



import com.smz.entity.PageEntity;
import com.smz.entity.Role;
import com.smz.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class RoleController {


    @Autowired
    private RoleService roleService;

    @ApiOperation("分页查询角色")
    @GetMapping("/roles/page")
    public ResponseEntity<PageEntity<Role>> findRolesPage(@RequestParam(value = "current",required = false,defaultValue = "1")
                                                                      Integer current){
        PageEntity<Role> page = roleService.findRolesPage(current);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/roles")
    public ResponseEntity<List<Role>> findAll(){
        List<Role> allRole = roleService.findAllRole();
        return ResponseEntity.ok(allRole);
    }
    @PostMapping("role")
    public ResponseEntity<String>addRole(@RequestBody Role role){
        roleService.insertRole(role);
        return ResponseEntity.ok("ok");
    }

    @PutMapping("role")
    public ResponseEntity<String>updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return ResponseEntity.ok("ok");
    }


    @DeleteMapping("role/{id}")
    public ResponseEntity<String>deleteRole(@PathVariable("id")Integer id){
        roleService.deleteRole(id);
        return ResponseEntity.ok("ok");
    }

    @GetMapping("role/{username}")
    public ResponseEntity<List<Role>> findById(@PathVariable("username") String  username){
        List<Role> roleById = roleService.selectAllRoleByName(username);
        return ResponseEntity.ok(roleById);
    }

}
